﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages.Reward_System
{
    /// <summary>
    /// Rewards.
    /// </summary>
    /// <remarks></remarks>
    public partial class Rewards : BSCMFF.Core.MultiLanguagePage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (EvalSelection.PublicSelectedValue != 0)
            {
                PanelRewards.Visible = true;

                BSCEntities context = new BSCEntities();
                Evaluation_Period ep = (from eps in context.Evaluation_Period
                                        where eps.ID == EvalSelection.PublicSelectedValue
                                        select eps).SingleOrDefault();

                if (ep.Competencies != true)
                {
                    RadGridCompetencies.Visible = false;
                }
                else
                {
                    RadGridCompetencies.Visible = true;
                    FillTopEmployeesCompetencyGrid(EvalSelection.PublicSelectedValue);
                }     
            }
        }

        /// <summary>
        /// Fills the PKPIs bonus grid.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <remarks></remarks>
        private void FillPKPIsBonusGrid(int evalID)
        {
        
        }

        /// <summary>
        /// Fills the top employees competency grid.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <remarks></remarks>
        private void FillTopEmployeesCompetencyGrid(int evalID)
        {
            BSCEntities context = new BSCEntities();
            Evaluation_Period ep;

            if (EvalSelection.PublicSelectedValue == 0)
            {
                return;
            }
            else
            {
                ep = (from temp in context.Evaluation_Period
                                        where temp.ID == evalID
                                        select temp).SingleOrDefault();
            }
            if (ep.Reward != true)
            {
                return;
            }

            var data = (from ev in context.Evaluation_User_Question_Value
                        join ee in context.Evaluation_Evaluator_Employee on ev.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        join eq in context.Evaluation_Question on ev.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        group ev by new {ee.Employee_FK} into g
                        join u in context.Users on g.Key.Employee_FK equals u.ID
                        join epp in context.Evaluation_Period on evalID equals epp.ID
                        orderby g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min) descending
                        select new
                        {
                            Fullname = u.Firstname + u.Lastname,
                            perc = g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min)*100/g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min),
                        
                            salary = u.Salary == null ? 0 : (int)u.Salary,
                            bonus = u.Salary == null ? 0 : (int)(u.Salary * epp.CompetencyCoef / 100)
                        }).ToList();
            
            int topPercent = (int)ep.CompetencyTop;
            int coef = (int)ep.CompetencyCoef;

            int totalEvaluatedEmployees = data.Count;

            int i = 0;

            foreach (var d in data)
            {
                if (i++ < 1)
                {
                    string fn = d.Fullname;
                }
            }
            RadGridCompetencies.DataSource = data;
            RadGridCompetencies.DataBind();
        }
    }
}